layui.use(['layer','form'], function(){
	var layer = layui.layer;
	var form = layui.form;

	/****************************** 营业员 ********************************/

	/*@维修单状态修改*/
	repair_state_fun = function(obj, level){
		$.post('/repair/repair/update_repair_state',obj,function(msg){

			if(msg.code === 1){
				if(level == 2){
					location.reload()
            		parent.layui.table.reload('table_list')
				}
				if(level == 3){
					xadmin.father_reload();
            		parent.parent.layui.table.reload('table_list')
				}
				if(level == 'ref'){
					layer.alert('本次退还完成',{icon: 6, title: '退还完成'},function(){
						location.reload()
            			parent.layui.table.reload('table_list')
					});
				}
            }else{
            	layer.alert(msg.msg, {icon: 5, title: '错误提示'})

            }
		})
	}
	/*@等待维修配件*/
	wait_parts = function(ids){
		layer.confirm('确认等待维修配件',function(){
			var level = 2
			var obj = {
				repair_id: ids,
				ty: 1
			} 
			repair_state_fun(obj, level)
		})
	}
	/*@确认维修信息*/
	query_repair = function(ids){
		layer.confirm('本次维修信息是否确认',function(){
			var level = 2
			var obj = {
				repair_id: ids,
				ty: 3
			} 
			repair_state_fun(obj, level)
		})
	}
	/*@维修外送*/
	out_repair = function(ids){
		layer.confirm('是否确定该订单外送维修',function(){
			var level = 2
			var obj = {
				repair_id: ids,
				ty: 5
			} 
			repair_state_fun(obj, level)
		})
	}
	/*@付款并取机*/
	pay_repair = function(ids, stay_money){
		if(Number(stay_money) > 0){
			layer.msg('未全额付款,不能进行该操作', {icon: 5, time: 1000})
			return
		}
		layer.confirm('确认用户是否支付全额维修金，并取机',function(){
			var level = 2
			var obj = {
				repair_id: ids,
				ty: 8
			} 
			repair_state_fun(obj, level)
		})
	}
	/*@退款并取机*/
	refund_repair = function(ids){
		layer.confirm('确认退还维修金并返还手机',function(){
			var level = 'ref'
			var obj = {
				repair_id: ids,
				ty: 7
			} 
			repair_state_fun(obj, level)
		})
	}
	/*@客户送机*/
	form.on('submit(send_mach)',function(data){
		console.log(data)
		var level = 3
		var obj = data.field
		obj.ty = 2
		repair_state_fun(obj, level)
	});
	/*@维修取消*/
	form.on('submit(cancel)',function(data){
		var level = 3
		var obj = data.field
		obj.ty = 10
		repair_state_fun(obj, level)
	});
	/*@恢复维修*/
	renew_repair = function(ids){
		layer.confirm('确认恢复该维修单',function(){
			var level = 2
			var obj = {
				repair_id: ids,
				ty: 'renew'
			} 
			repair_state_fun(obj, level)
		})
	}
	/*编辑维修金额按钮*/
	up_box = function(ids, money, actual_money){
		console.log(1111)
		if($('.up-input').is(':hidden')){/*切换修改*/
			$('.up-span').css('display','none')
			$('.up-input').css('display','inline-block')
		}else{/*确认修改*/

			let repair_money = $('input[name="repair_money"]').val()
			let stay_money = Number(repair_money) - Number(actual_money)
			let people_money = Number(repair_money) - Number(money)
			if(valite_money(repair_money, money, actual_money) === false){
				return
			}
			let objData = {
				repair_id: ids,
				repair_money: repair_money,
				stay_money: stay_money,
				people_money: people_money,
			}
			$.post('/repair/repair/up_money', objData, function(data){
				if(data.code !== 1){
					layer.msg(data.msg, {icon: 5})
				}else{
					location.reload();
				}
			})
		}
	}
	/*@维修金额编辑*/
	write_repair_money = function(obj, money, actual_money){
		let repair_money = $(obj).val()
		if(valite_money(repair_money, money, actual_money) === false){
			return
		}
		$('.stay_money').text(Number(repair_money) - Number(actual_money))
		$('.people_money').text(Number(repair_money) - Number(money))
	}
	/*@维修金额验证*/
	valite_money = function(repair_money, money, actual_money){
		if(Number(money) > Number(repair_money)){
			layer.msg('维修总金额不得小于配件费用', {icon: 5});
			return false
		}
		if(Number(actual_money) > Number(repair_money)){
			layer.msg('维修总金额不得小于已支付金额', {icon: 5});
			return false
		}
	}
	/****************************** 维修员 ********************************/
	/*@接受维订单*/
	accept_repair = function(ids){
		layer.confirm('您确认接受此次维修订单',function(){
			var level = 2
			var obj = {
				repair_id: ids,
				ty: 4
			} 
			repair_state_fun(obj, level)
		})
	}
	/*@维修失败*/
	form.on('submit(fail)',function(data){
		var level = 3
		var obj = data.field
		obj.ty = 9
		repair_state_fun(obj, level)
	});
	/*@维修成功完成*/
	form.on('submit(finish)',function(data){
		var level = 3
		var obj = data.field
		obj.ty = 6
		repair_state_fun(obj, level)
	});
	/*@添加维修进度*/
	form.on('submit(repair_log)',function(data){
		$.post("/log/repair_log/add",data.field,function(msg){
			if(msg.code === 1){
				layer.msg(msg.msg, {icon: 6, time: 1000}, function(){
					// layer.close()
					xadmin.father_reload();
				})
			}else{
				layer.msg(msg.msg, {icon: 5, time: 1000})
			}
		})
	});
	/*@删除维修进度*/
	del_log = function (obj1,ids){
		layer.confirm('确认删除',function(){
			var obj = {ids: ids};
			$.get("/log/repair_log/del",obj,function(msg){
				if(msg.code === 1){
					layer.msg(msg.msg, {icon: 6, time: 1000}, function(){
						$(obj1).parents('.every_log').remove()
					})
				}else{
					layer.msg(msg.msg, {icon: 5, time: 1000})
				}
			})
		})
	}
	
})